package org.zakariya.stickyheadersapp.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import org.zakariya.stickyheaders.ActionType;
import org.zakariya.stickyheaders.Constant;
import org.zakariya.stickyheaders.StickyHeaderLayoutManager;
import org.zakariya.stickyheaders.Utils;
import org.zakariya.stickyheaders.callback.ClickCallback;
import org.zakariya.stickyheaders.model.HeaderModel;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheadersapp.ResourceTable;
import org.zakariya.stickyheadersapp.adapter.StressTestAdapter;
import org.zakariya.stickyheadersapp.tool.IReload;
import org.zakariya.stickyheadersapp.tool.TopToolUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * StressTestSlice
 *
 * @since 2021-08-20
 */
public class StressTestSlice extends AbilitySlice {
    private StressTestAdapter adapter;
    private List<ItemModel> dataList = new ArrayList<>();
    private List<ItemModel> temp = new ArrayList<>();
    private List<ItemModel> copyData = new ArrayList<>();
    private ItemModel model = null;
    private int numSections;
    private int numItemsPerSection;
    private Image iv_bottom;
    private Image iv_remove;
    private Image iv_menu;
    private ListContainer listContainer;
    private ActionType actionType;
    private StickyHeaderLayoutManager headerDecor;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_stress_test);
        initComponent();
    }

    private void initComponent() {
        iv_bottom = (Image) findComponentById(ResourceTable.Id_iv_bottom);
        iv_remove = (Image) findComponentById(ResourceTable.Id_iv_remove);
        iv_menu = (Image) findComponentById(ResourceTable.Id_iv_menu);
        iv_bottom.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showScrollTool(getContext(), listContainer);
            }
        });

        iv_remove.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showViewTool(getContext(), listContainer, dataList);
            }
        });

        iv_menu.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                TopToolUtils.showReloadTool(getContext(), listContainer, new IReload() {
                    @Override
                    public void reloadData() {
                        headerDecor.setDataList(dataList, false);
                        adapter.notifyDataChanged();
                    }
                });
            }
        });
        Image image = (Image) findComponentById(ResourceTable.Id_icon_back);
        image.setClickedListener(component -> terminateAbility());
        Text headerText = (Text) findComponentById(ResourceTable.Id_sticky_text);
        Text indexText = (Text) findComponentById(ResourceTable.Id_index_text);
        Button cloneBtn = (Button) findComponentById(ResourceTable.Id_cloneBtn);
        Button deleteBtn = (Button) findComponentById(ResourceTable.Id_deleteBtn);
        DependentLayout layout_overlap = (DependentLayout) findComponentById(ResourceTable.Id_layout_overlap);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_collapsing_headers);
        numSections = 1000;
        numItemsPerSection = 5;
        dataList = Utils.getStickyList(numSections, numItemsPerSection, true, HeaderModel.TYPE_70vp);
        adapter = new StressTestAdapter(this, dataList, mClickCallback);
        listContainer.setItemProvider(adapter);
        headerDecor = new StickyHeaderLayoutManager(listContainer, layout_overlap, headerText, indexText, cloneBtn, deleteBtn, mClickCallback);
        headerDecor.setDataList(dataList, false);
    }

    private final ClickCallback mClickCallback = (index, type, clickSource, button) -> {
        int mPos = 0;
        if (clickSource.equals(Constant.ISOTHER)) {
            //点击的是Header
            for (int i = 0; i < dataList.size(); i++) {
                if (dataList.get(i).isHeader()) {
                    if (dataList.get(i).getTitle().equals(dataList.get(index).getTitle())) {
                        mPos = i;
                    }
                }
            }
        } else {
            mPos = index;
        }
        startAction(mPos, type);
    };

    private void startAction(int mPos, ActionType type) {
        actionType = type;
        switch (type) {
            case CLONE:
                addCopy(mPos);
                break;
            case DELETE:
                delete(mPos);
                break;
        }
    }

    private void delete(int mPos) {
        boolean isHeader = dataList.get(mPos).isHeader();
        int position = 0;
        if (isHeader) {
            for (int i = mPos; i < dataList.size(); i++) {
                if (i > mPos) {
                    if (dataList.get(i).isHeader()) {
                        break;
                    }
                }
                position = i;
                copyData.add(dataList.get(i));
            }
            listContainer.getItemProvider().notifyDataSetItemRangeRemoved(mPos, position - mPos + 1);
            dataList.removeAll(copyData);
            adapter.notifyDataChanged();
            for (int i = 0; i < dataList.size(); i++) {
                if (i >= mPos) {
                    if (dataList.get(i).isHeader()) {
                        HeaderModel model = (HeaderModel) dataList.get(i);
                        HeaderModel headerModel = null;
                        headerModel = new HeaderModel(model.getTitle(), model.getMessage(), model.getType(),
                                Math.max((model.getIndex() - (copyData.size() + 1)), 0), Math.max((model.getIndex() - (copyData.size() + 1)), 0));
                        dataList.remove(i);
                        dataList.add(i, headerModel);
                    } else if (dataList.get(i).isEnd()) {
                        ItemModel model = dataList.get(i);
                        ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                false, true, false, false, Math.max((model.getIndex() - (copyData.size() + 1)), 0), Math.max((model.getIndex() - (copyData.size() + 1)), 0));
                        dataList.remove(i);
                        dataList.add(i, model1);
                    } else {
                        ItemModel model = dataList.get(i);
                        ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                false, false, false, true, Math.max((model.getIndex() - (copyData.size() + 1)), 0), Math.max((model.getIndex() - (copyData.size() + 1)), 0));
                        dataList.remove(i);
                        dataList.add(i, model1);
                    }
                }
            }
        } else {
            copyData.add(dataList.get(mPos));
            listContainer.getItemProvider().notifyDataSetItemRemoved(mPos);
            dataList.removeAll(copyData);
            adapter.notifyDataChanged();
            for (int i = 0; i < dataList.size(); i++) {
                if (i >= mPos) {
                    if (dataList.get(i).isHeader()) {
                        HeaderModel model = (HeaderModel) dataList.get(i);
                        HeaderModel headerModel = null;
                        headerModel = new HeaderModel(model.getTitle(), model.getMessage(), model.getType(),
                                model.getIndex() - 1, model.getIndex() - 1);
                        dataList.remove(i);
                        dataList.add(i, headerModel);
                    } else if (dataList.get(i).isEnd()) {
                        ItemModel model = dataList.get(i);
                        ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                false, true, false, false, model.getIndex() - 1, model.getIndex() - 1);
                        dataList.remove(i);
                        dataList.add(i, model1);
                    } else {
                        ItemModel model = dataList.get(i);
                        ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                false, false, false, true, model.getIndex() - 1, model.getIndex() - 1);
                        dataList.remove(i);
                        dataList.add(i, model1);
                    }
                }
            }
        }
        copyData.clear();
    }

    private void addCopy(int mPos) {
        boolean isHeader = dataList.get(mPos).isHeader();
        String copyName = "";
        int endPosition = 0;
        if (isHeader) {
            temp.clear();
            for (int i = mPos; i < dataList.size(); i++) {
                if (dataList.get(i).isEnd()) {
                    copyData.add(dataList.get(i));
                    endPosition = i;
                    break;
                }
                copyData.add(dataList.get(i));
            }
            for (ItemModel copyDatum : copyData) {
                if (copyDatum.isHeader()) {
                    if (copyDatum.getTitle().contains("copy")) {
                        int copyNum = Integer.parseInt(copyDatum.getTitle().split("y ")[1]) + 1;
                        copyName = copyDatum.getTitle().split("y ")[0] + "y " + copyNum;
                    } else {
                        copyName = copyDatum.getTitle() + "  copy " + 1;
                    }
                }
            }
            for (ItemModel copyDatum : copyData) {
                if (copyDatum.isHeader()) {
                    HeaderModel headerModel = new HeaderModel(copyName, copyDatum.getMessage(), HeaderModel.TYPE_70vp, copyDatum.getIndex(), copyDatum.getIndex());
                    temp.add(headerModel);
                } else if (copyDatum.isEnd()) {
                    ItemModel itemModel = new ItemModel(copyDatum.getTitle() + "/" + copyName, copyDatum.getMessage(),
                            false, true, false, false, copyDatum.getIndex(), copyDatum.getIndex());
                    temp.add(itemModel);
                } else {
                    copyName = " c" + copyName.split(" c")[1];
                    ItemModel itemModel = new ItemModel(copyDatum.getTitle(), copyDatum.getMessage() + copyName,
                            false, false, false, true, copyDatum.getIndex(), copyDatum.getIndex());
                    temp.add(itemModel);
                }
            }
            dataList.addAll(endPosition + 1, temp);
            for (int i = endPosition + 1; i < dataList.size(); i++) {
                if (dataList.get(i).isHeader()) {
                    HeaderModel model = (HeaderModel) dataList.get(i);
                    HeaderModel headerModel = new HeaderModel(model.getTitle(), model.getMessage(), model.getType(), model.getIndex() + copyData.size() + 1, model.getIndex() + copyData.size() + 1);
                    dataList.remove(i);
                    dataList.add(i, headerModel);
                } else if (dataList.get(i).isEnd()) {
                    ItemModel model = dataList.get(i);
                    ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                            false, true, false, false, model.getIndex() + copyData.size() + 1, model.getIndex() + copyData.size() + 1);
                    dataList.remove(i);
                    dataList.add(i, model1);
                } else {
                    ItemModel model = dataList.get(i);
                    ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                            false, false, false, true, model.getIndex() + copyData.size() + 1, model.getIndex() + copyData.size() + 1);
                    dataList.remove(i);
                    dataList.add(i, model1);
                }
            }
            adapter.notifyDataChanged();
            temp.clear();
        } else {
            copyData.add(dataList.get(mPos));
            temp.clear();
            temp.addAll(dataList);
            for (int tempPosition = 0; tempPosition < temp.size(); tempPosition++) {
                for (ItemModel copyDatum : copyData) {
                    if (temp.get(tempPosition).equals(copyDatum)) {
                        ItemModel itemModel = new ItemModel(copyDatum.getTitle(), copyDatum.getMessage() + "  copy",
                                false, false, false, true, copyDatum.getIndex(), copyDatum.getIndex());
                        dataList.add(tempPosition + 1, itemModel);
                        for (int i = tempPosition + 1; i < dataList.size(); i++) {
                            if (dataList.get(i).isHeader()) {
                                HeaderModel model = (HeaderModel) dataList.get(i);
                                HeaderModel headerModel = new HeaderModel(model.getTitle(), model.getMessage(), model.getType(), model.getIndex() + 1, model.getIndex() + 1);
                                dataList.remove(i);
                                dataList.add(i, headerModel);
                            } else if (dataList.get(i).isEnd()) {
                                ItemModel model = dataList.get(i);
                                ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                        false, true, false, false, model.getIndex() + 1, model.getIndex() + 1);
                                dataList.remove(i);
                                dataList.add(i, model1);
                            } else {
                                ItemModel model = dataList.get(i);
                                ItemModel model1 = new ItemModel(model.getTitle(), model.getMessage(),
                                        false, false, false, true, model.getIndex() + 1, model.getIndex() + 1);
                                dataList.remove(i);
                                dataList.add(i, model1);
                            }
                        }
                        model = copyDatum;
                    }
                }
            }
        }
        copyData.remove(model);
        adapter.notifyDataChanged();
        copyData.clear();
    }
}
